﻿using UnityEngine;
using System.Collections;

namespace Room
{
    public class PlayerAIStateNormal : PlayerAIStateBase
    {
        GameObject fishObj;
        GameObject mineObj;
        protected override void OnEnter()
        {
            if (long.Parse(controller.owner.name) == StaticData.playerData.playerId)
            {
                EasyTouchTool.Instance.ShowJoystick();
                UI.UIFieldRunMap.RefreshPage();
            }

            var Actor = controller as RCAIActor;
            fishObj = Actor.fishObj;
            mineObj = Actor.mineObj;

            HideLifeUtils();
        }

        protected override void OnLeave()
        {
            if (long.Parse(controller.owner.name) == StaticData.playerData.playerId)
            {
                EasyTouchTool.Instance.Hide();
            }
        }

        void HideLifeUtils()
        {
            if (fishObj != null)
            {
                var resName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.FishModel;
                ResourceLoader.GetInstance().weaponLoader.UnloadWeapon(fishObj, resName);
            }
            if (mineObj != null)
            {
                var resName = Dict.DictDataManager.Instance.tableGameConfigureDao.currentData.MiningHoe;
                ResourceLoader.GetInstance().weaponLoader.UnloadWeapon(mineObj, resName);
                ((RCAIActor)controller).mineObj = null;
                mineObj = null;
            }
        }

        bool rebornEff = false;
        float invincibleTimer = 0;
        public float InvincibleTimer//无敌的时间
        {
            get
            {
                return invincibleTimer;
            }
            set
            {
                invincibleTimer = value;

            }
        }

        protected override void OnTick()
        {
            base.OnTick();

            if (InvincibleTimer > 0f)
            {
                InvincibleTimer -= Time.deltaTime;

            }

            if (rebornEff && InvincibleTimer <= 0f && light0 != null)
            {
                GameObjectPoolManager.Instance.Unspawn(light0);
                rebornEff = false;
            }

            currTimer += Time.deltaTime;
            if (currTimer >= fixedTime)
            {
                currTimer = 0f;
                HideLifeUtils();
            }
        }
        float currTimer = 0f;
        const float fixedTime = 2f;


        GameObject light0;
        public void PlayReborn()
        {
            if (light0 != null)
            {
                GameObjectPoolManager.Instance.Unspawn(light0);
            }

            light0 = GameObjectPoolManager.Instance.Spawn(RoomField.particleNoEnemy, CommonMethod.GetAssetNameFromAssetBundleName(RoomField.particleNoEnemy));
            light0.transform.SetParent(controller.owner.transform);
            light0.transform.localScale = Vector3.one;
            light0.transform.localPosition = new Vector3(0, 0f, 0);
            light0.transform.localRotation = Quaternion.Euler(-90, 0, 0);
            light0.SetActive(true);
            rebornEff = true;
        }
    }
}